//
//  CinemaViewController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/19.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "CinemaViewController.h"
#import "DataService.h"
#import "CinemaTableViewCell.h"
#include "CinemaListModel.h"

@interface CinemaViewController ()
{
    BOOL isopen[200];
}

@end

@implementation CinemaViewController

//
//{
//    "id": "1029",
//    "name": "东城区"
//},

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self)
    {
        self.title = @"电影院";
        
    }
    
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self configureCinemaTable];
    
    // Do any additional setup after loading the view.
}

- (void)configureCinemaTable
{
    _cinema = [[UITableView alloc]initWithFrame:self.view.bounds];
    _cinema.dataSource = self;
    _cinema.delegate = self;
    
    UINib *nib = [UINib nibWithNibName:@"CinemaTableViewCell" bundle:[NSBundle mainBundle]];
    [_cinema registerNib:nib forCellReuseIdentifier:@"CinemaCell"];
    
    [self.view addSubview:_cinema];
}

- (void)loadData
{
    _district = [[NSArray alloc]init];
   
   NSDictionary * dic = [DataService getJsonDataFromFile:@"district_list.json"];
    _district = [dic valueForKey:@"districtList"];
    
    _cinemaList = [[NSArray alloc]init];
    
   NSDictionary * cin = [DataService getJsonDataFromFile:@"cinema_list.json"];
    _cinemaList = [cin valueForKey:@"cinemaList"];
    
    cinemaOfDistrict = [[NSMutableDictionary alloc]init];
    
    for (int i = 0 ;i < [_district count];i++)
    {
        NSMutableArray *temp = [[NSMutableArray alloc]init];
        for (NSDictionary *dic2 in _cinemaList)
        {
            if ([[_district[i] valueForKey:@"id"] isEqualToString:[dic2 valueForKey:@"districtId"]])
            {
                [temp addObject:dic2];
            }
        }
        [cinemaOfDistrict setObject:temp forKey:[NSString stringWithFormat:@"%i",i]];

    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_district count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    int count = 0;
    if(isopen[section])
    {
        
        for (NSDictionary *dic in _cinemaList) {
            if ([[_district[section] valueForKey:@"id"] isEqualToString:[dic valueForKey:@"districtId"]]) {
                count++;
              //  [temp addObject:dic];
            }
        }
       // [cinemaOfDistrict setObject:temp forKey:[NSString stringWithFormat:@"%li",section]];
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaCell"];
    CinemaListModel *model = [[CinemaListModel alloc]init];
    
    NSArray *temp = [cinemaOfDistrict objectForKey:[NSString stringWithFormat:@"%li",indexPath.section]];
    NSDictionary *dic = temp[indexPath.row];
    
    [model setValuesForKeysWithDictionary:dic];
    cell.cinemaModel = model;
    return cell;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *button = [[UIButton alloc] init];
    
    
    NSString *groupName = [_district[section] objectForKey:@"name"];
    
    [button setBackgroundImage:[UIImage imageNamed:@"btnbg_blue"] forState:UIControlStateNormal];
    [button setTitle:groupName forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button.tag = section;
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return button;


}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 89;
}

- (void)btnAction:(UIButton *)but
{
    isopen[but.tag] = !isopen[but.tag];
    
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:but.tag];
    
    [_cinema reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
